﻿using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace LdtJudge.DataEngine.Common.MoneyAssemble
{
    /// <summary>
    /// 金额格式化处理类
    /// </summary>
    public class LdtMoneyFormat
    {
        /// <summary>
        /// 转换数字
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns>System.Int64.</returns>
        private static long CharToNumber(char c)
        {
            switch (c)
            {
                case '壹': return 1;
                case '贰': return 2;
                case '叁': return 3;
                case '肆': return 4;
                case '伍': return 5;
                case '陆': return 6;
                case '柒': return 7;
                case '捌': return 8;
                case '玖': return 9;
                case '一': return 1;
                case '两': return 2;
                case '二': return 2;
                case '三': return 3;
                case '四': return 4;
                case '五': return 5;
                case '六': return 6;
                case '七': return 7;
                case '八': return 8;
                case '九': return 9;
                case '零': return 0;
                default: return -1;
            }
        }

        /// <summary>
        /// 转换单位
        /// </summary>
        /// <param name="c">The c.</param>
        /// <returns>System.Int64.</returns>
        private static long CharToUnit(char c)
        {
            switch (c)
            {
                case '拾': return 10;
                case '佰': return 100;
                case '仟': return 1000;
                case '萬': return 10000;
                case '十': return 10;
                case '百': return 100;
                case '千': return 1000;
                case '万': return 10000;
                case '亿': return 100000000;
                default: return 1;
            }
        }
        /// <summary>
        /// 将中文数字转换阿拉伯数字
        /// </summary>
        /// <param name="cnum">汉字数字</param>
        /// <returns>长整型阿拉伯数字</returns>
        public static long ParseCnToInt(string cnum)
        {
            cnum = Regex.Replace(cnum, "\\s+", "");

            /****************************增加兼容传入的为数字进行转换,目前默认为最多支持20个 ljx 2017-08-10 *********开始**********/
            List<string> convertToNumber = new List<string>() { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20" };
            if (convertToNumber.Contains(cnum))
                return Convert.ToInt64(cnum);
            /****************************增加兼容传入的为数字进行转换，目前默认为最多支持20个 ljx 2017-08-10 **********结束*********/

            long firstUnit = 1;//一级单位                  
            long secondUnit = 1;//二级单位   
            long tmpUnit = 1;//临时单位变量  
            long result = 0;//结果  
            for (int i = cnum.Length - 1; i > -1; --i)//从低到高位依次处理  
            {
                tmpUnit = CharToUnit(cnum[i]);//取出此位对应的单位  
                if (tmpUnit > firstUnit)//判断此位是数字还是单位  
                {
                    firstUnit = tmpUnit;//是的话就赋值,以备下次循环使用  
                    secondUnit = 1;
                    if (i == 0)//处理如果是"十","十一"这样的开头的  
                    {
                        result += firstUnit * secondUnit;
                    }
                    continue;//结束本次循环  
                }
                else if (tmpUnit > secondUnit)
                {
                    secondUnit = tmpUnit;
                    continue;
                }
                result += firstUnit * secondUnit * CharToNumber(cnum[i]);//如果是数字,则和单位想乘然后存到结果里  
            }
            return result;
        }
        /// <summary>
        /// 转化汉语表达的数字为阿拉伯数字
        /// </summary>
        /// <param name="AChineseNumbText">汉语数字</param>
        /// <returns>阿拉伯数字。</returns>
        public static double DecodeNumberCn(string AChineseNumbText)
        {
            try
            {
                double resultValue;
                if (double.TryParse(AChineseNumbText, out resultValue))
                {
                    return resultValue;         //本身就是double，则直接返回
                }

                if (AChineseNumbText == string.Empty)
                    return 0;

                AChineseNumbText = AChineseNumbText.Replace("亿亿", "兆");
                AChineseNumbText = AChineseNumbText.Replace("万万", "亿");
                AChineseNumbText = AChineseNumbText.Replace("点", "元");
                AChineseNumbText = AChineseNumbText.Replace("块", "元");
                AChineseNumbText = AChineseNumbText.Replace("毛", "角");
                AChineseNumbText = AChineseNumbText.Replace("个", "");
                AChineseNumbText = AChineseNumbText.Replace("〇", "零");
                AChineseNumbText = AChineseNumbText.Replace(",", "");           //对格式“1,123,456,789”中的逗号进行替换

                double vResult = 0;
                double vIntegerTemp = 0;              //整数部分缓存值
                double vNumber = 0;
                string tempString = String.Empty;// 当前数字 

                foreach (char vChar in AChineseNumbText)
                {
                    int i = "零一二三四五六七八九".IndexOf(vChar);
                    if (i < 0) i = "洞幺两三四五六拐八勾".IndexOf(vChar);
                    if (i < 0) i = "零壹贰叁肆伍陆柒捌玖".IndexOf(vChar);

                    if (Regex.IsMatch(vChar.ToString(), @"\d"))
                        i = Convert.ToInt32(vChar.ToString());//LdFormat.ObjectToInt(vChar.ToString());

                    if (i == 0)
                    {
                        vIntegerTemp = vIntegerTemp * 10;           //整数位*10;                    
                        continue;
                    }

                    if (i > 0)
                    {
                        if (string.IsNullOrEmpty(tempString))
                        {
                            vNumber = i;

                            vIntegerTemp = vIntegerTemp * 10; //整数位*10;
                            vIntegerTemp += vNumber; //把当前数字追加到当前值序列
                        }
                        else
                        {
                            vIntegerTemp = Convert.ToDouble(tempString + i);
                            tempString += i;
                        }
                    }
                    else
                    {
                        if (vChar.ToString().Equals(".")) //处理小数点
                        {
                            tempString = vIntegerTemp.ToString() + ".";
                        }
                        else
                        {
                            //处理单位
                            i = "元十百千万亿".IndexOf(vChar);
                            if (i < 0) i = "整拾佰仟万亿兆".IndexOf(vChar);
                            if (i == 5) i = 8;
                            if (i == 6) i = 12;
                            if (i > 0)
                            {
                                //当首字符为单位时，默认其数学值为1
                                if (vResult == 0 && vIntegerTemp == 0)
                                    vIntegerTemp = 1;

                                vIntegerTemp = vIntegerTemp * Math.Pow(10, i); ;           //整数位*10;

                                //每处理一个单位，就缓存一次前边的结果值；
                                vResult += vIntegerTemp;
                                vIntegerTemp = 0;
                            }
                            else
                            {
                                i = "元角分".IndexOf(vChar);
                                if (i > 0)
                                {
                                    vIntegerTemp = vIntegerTemp * Math.Pow(10, -i);
                                }
                                //每处理一个单位，就缓存一次前边的结果值；
                                vResult += vIntegerTemp;
                                vIntegerTemp = 0;

                                continue;
                            }
                        }
                    }
                }

                return vResult + vIntegerTemp;
            }
            catch
            {
                return 0.0;
            }
        }

        /// <summary>
        /// 转换阿拉伯数字转大写
        /// </summary>
        /// <param name="num">阿拉伯数字</param>
        /// <returns>返回大写形式</returns>
        public static string CmycurD(int num)
        {

            string str1 = "零一二三四五六七八九";            //0-9所对应的汉字
            string str2 = "个十百千万亿"; //数字位所对应的汉字 
            string str3 = "";    //从原num值中取出的值
            string str4 = num.ToString();    //数字的字符串形式
            string str5 = "";  //返回大写内容  
            string ch1 = "";    //数字的汉语读法
            string ch2 = "";    //数字位的汉字读法
            int nzero = 0;  //用来计算连续的零值是几个
            int temp;            //从原num值中取出的值

            int j = str4.Length;      //找出最高位 

            //循环取出每一位需要转换的值
            for (int i = 0; i < j; i++)
            {
                str3 = str4.Substring(i, 1);          //取出需转换的某一位的值
                temp = Convert.ToInt32(str3);      //转换为数字
                if (i != (j - 7) && i != (j - 11) && i != (j - 15))
                {
                    if (str3 == "0")
                    {
                        ch1 = "";
                        ch2 = "";
                        nzero = nzero + 1;
                    }
                    else
                    {
                        if (str3 != "0" && nzero != 0)
                        {
                            ch1 = "零" + str1.Substring(temp * 1, 1);
                            ch2 = str2.Substring(j - 1 - i, 1);
                            nzero = 0;
                        }
                        else
                        {
                            ch1 = str1.Substring(temp, 1);//截取数字位
                            ch2 = str2.Substring(j - 1 - i, 1);
                            nzero = 0;
                        }
                    }
                }
                if (ch2 == "个")
                {
                    ch2 = string.Empty;
                }
                str5 = str5 + ch1 + ch2;
            }

            return str5;
        }


        #region 汉字转数字
        /// <summary>
        /// 将字符转为数字
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string CharToNumber(string c)
        {
            switch (c)
            {
                case "一": return "1";
                case "二": return "2";
                case "三": return "3";
                case "四": return "4";
                case "五": return "5";
                case "六": return "6";
                case "七": return "7";
                case "八": return "8";
                case "九": return "9";
                case "零": return "0";
                case "两": return "2";
                case "俩": return "2";
                default: return c;
            }
        }
        /// <summary>
        /// 将数字转字符
        /// </summary>
        /// <param name="c"></param>
        /// <returns></returns>
        public static string NumberToChar(string c)
        {
            switch (c)
            {
                case "1": return "一";
                case "2": return "二";
                case "3": return "三";
                case "4": return "四";
                case "5": return "五";
                case "6": return "六";
                case "7": return "七";
                case "8": return "八";
                case "9": return "九";
                case "零": return "0";
                case "两": return "2";
                case "俩": return "2";
                default: return c;
            }
        }
        #endregion


    }
}
